/*
 * VHJsData.java
 * Created on 25 mai 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.utils;

import java.util.Date;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseTreeModel;
import com.google.gwt.user.client.rpc.IsSerializable;

/**
 * Generic data object which is used to encapsulate a server side DAO when it needs to be
 * used on the client (JavaScript) side.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHJsData extends BaseTreeModel implements IsSerializable
{
  public static final String PARAM_TYPE = "$type$";
  public static final String PARAM_ID   = "id";

  /** No-arg constructor for GWT/RPC. */
  public VHJsData()
  {}

  /**
   * Creates a new VHJsData object adding the list of items as children.
   *
   * @param listChildren the child items to be added immediately
   */
  public VHJsData( List<VHJsData> listChildren )
  {
    addAll( listChildren );
  }

  /**
   * Indicates whether the data item is bound to a remote server object.
   */
  public boolean isRemote()
  {
    return getType() != null;
  }

  /**
   * Returns the name of the server side class this object is based on.
   */
  public String getType()
  {
    return get( PARAM_TYPE );
  }

  /**
   * Returns the unique item ID as string (the value of the "id" property).
   */
  public String getId()
  {
    return String.valueOf( get( PARAM_ID ) );
  }

  public void addAll( List<VHJsData> list )
  {
    for ( VHJsData data : list )
      add( data );
  }

  public String getString( String strKey )
  {
    return (String) get( strKey );
  }

  public Date getDate( String strKey )
  {
    return (Date) get( strKey );
  }

  public boolean getBoolean( String strKey )
  {
    Boolean b = (Boolean) get( strKey );

    if ( b == null )
      return false;

    return b.booleanValue();
  }

  public int getInt( String strKey )
  {
    Integer n = (Integer) get( strKey );

    return n != null ? n.intValue() : 0;
  }

  public double getDouble( String strKey )
  {
    Double d = (Double) get( strKey );

    return d != null ? d.doubleValue() : 0.0;
  }

  /**
   * Renames a property.
   *
   * @param strOldName the name of the property to be renamed
   * @param strNewName the new name of the property
   */
  public void renameProperty( String strOldName, String strNewName )
  {
    Object val = remove( strOldName );
    set( strNewName, val );
  }

  /**
   * Renames all properties whose names start with the given prefix. The prefix is
   * replaced by the new one.
   *
   * @param strOldName the name of the property to be renamed
   * @param strNewName the new name of the property
   */
  public void renamePropertiesWithPrefix( String strOldPrefix, String strNewPrefix )
  {
    for ( String strProperty : getPropertyNames() )
    {
      if ( strProperty.startsWith( strOldPrefix ) )
      {
        String strNewName;

        // If the property name is longer than the given prefix, we have to reuse part of
        // the name in the new name. Otherwise, we only use the new prefix.
        //
        if ( strProperty.length() > strOldPrefix.length() )
        {
          String strSuffix = strProperty.substring( strOldPrefix.length() );

          strNewName = strNewPrefix + strSuffix;
        }
        else
          strNewName = strNewPrefix;

        renameProperty( strProperty, strNewName );
      }
    }
  }

  /**
   * Returns all properties defined for this object in a string object (one property per
   * line).
   */
  public String dump()
  {
    StringBuilder sb = new StringBuilder();

    for ( String strProp : getProperties().keySet() )
    {
      if ( sb.length() > 0 )
        sb.append( ",\n" );

      sb.append( strProp + ": " + get( strProp ) );
    }

    return sb.toString();
  }
}
